package com.probiz.estoresa.system.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.AppAdmin;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.system.service.AppAdminManager;
import com.probiz.estore.webapp.util.RequestContext;


public class SelfProfileController extends GenericStoreAdminController<AppAdmin> {
    private AppAdminManager appAdminManager = null;
    
    public void setAppAdminManager(AppAdminManager appAdminManager) {
        this.appAdminManager = appAdminManager;
    }
    
	public ModelAndView defaultAction(HttpServletRequest request,
                                      HttpServletResponse response) {
    	Integer userId  = RequestContext.getCurrentUserId();
		AppAdmin appAdmin = appAdminManager.getById(userId);
		request.setAttribute("appAdmin", appAdmin);
		
		return new ModelAndView("selfProfileForm");
         
    }   
	
	
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
    	
    	// 取得Form对应的Model
		Integer userId  = RequestContext.getCurrentUserId();
		AppAdmin appAdmin = appAdminManager.getById(userId);

		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, appAdmin);
			errors = new BindException(binder.getBindingResult());
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}
		
		boolean isEmailExist=false;
        //check existence of the user's email
        isEmailExist=appAdminManager.getIsEmailUseByOthers(appAdmin.getEmail(),appAdmin.getAppuserId());
        if(isEmailExist){
            errors.rejectValue("email","appUser.email.isExist","the email has use by others");
            return this.showForm(request, errors);
	    }
        
      //add or update appadmin object.
        appAdminManager.save(appAdmin);
        
        String msgKey = (isEntityNew(request)) ? "common.added" : "common.updated";
		String message = getMessage(msgKey, new Object[] { getEntityTypeMessage(), getEntityName(appAdmin) });
		saveMessage(request, message);
		
		request.setAttribute("appAdmin", appAdmin);
		return new ModelAndView("selfProfileForm");
	}
	
	@Override
	protected String getEntityName(AppAdmin entity) {
		return entity.getUsername();
	}


	@Override
	protected void initController() throws Exception {
		this.mgr = appAdminManager;
		this.getServletContext().setAttribute("DEFAULT_ADMIN_USER_NAME", Constants.DEFAULT_ADMIN_USER_NAME);
	}

	@Override
	protected void preSave(HttpServletRequest request, AppAdmin entity, BindException errors) {
		
	}
  	  	
}
